package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.common.exception.BlogException;
import com.example.blog.common.pojo.dataobject.BlogInfo;
import com.example.blog.common.pojo.dataobject.UserInfo;
import com.example.blog.common.pojo.request.UserLoginParam;
import com.example.blog.common.pojo.response.UserInfoResponse;
import com.example.blog.common.pojo.response.UserLoginResponse;
import com.example.blog.common.utils.BeanConverse;
import com.example.blog.common.utils.JwtUtil;
import com.example.blog.common.utils.SecurityUtil;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/29
 * Time: 15:19
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse login(UserLoginParam userParam) {
        // 判断用户是否存在
        // 根据用户名查询用户信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userParam.getUserName())
                .eq(UserInfo::getDeleteFlag, 0));
        if (userInfo == null) {
            throw new BlogException("用户不存在");
        }
        // 用户存在，校验密码是否正确
        if (!SecurityUtil.verify(userParam.getPassword(), userInfo.getPassword())) {
            throw new BlogException("密码错误");
        }
        // 密码正确
        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(userInfo.getId());
        // 载荷
        Map<String, Object> map = new HashMap<>();
        map.put("id", userInfo.getId());
        map.put("name", userInfo.getUserName());
        response.setToken(JwtUtil.genToken(map));

        return response;
    }

    @Override
    public UserInfoResponse getUserInfoById(Integer userId) {
        UserInfo userInfo = selectUserInfoById(userId);
        return BeanConverse.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        // 1. 根据博客 id 拿到作者 id
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
        // 2. 根据作者 id 拿到作者详情
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());
        return BeanConverse.trans(userInfo);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId)
                .eq(UserInfo::getDeleteFlag, 0));
    }
}
